﻿class AnimePlayer extends PlayerBase {
    public play(): void {
        // GlobalAudio.play(this.paint.audio);
        //this.snd = this.getRes(this.paint.audio);
        //this.snd.play();

        SoundManager.playSound(QNService.getFullUrl(this.paint.audio),null);

        this.timer.addEventListener(egret.TimerEvent.TIMER, this.onTimer, this)
        this.timer.start();
    }

    public stop(): void {
        
    }

    public pause(): void {
    }

    public resume(): void {
    }

    public finish(): void {
        SoundManager.stopSound()
        //if (this.snd)
        //    this.snd.stop();
        this.timer.removeEventListener(egret.TimerEvent.TIMER, this.onTimer, this)
        this.timer.stop();
        this.dispatchEvent(new egret.Event(egret.Event.COMPLETE));
    }

    private onTimer(event: egret.TimerEvent): void {
        var frameArr: any[];
        var index: number;
        index = this.timer.currentCount;

        var crtTiem: number = index / 24;

        if (index < this.data.replayData.length) {
            frameArr = this.data.replayData[index];
            this.apply(frameArr);
        } else {
            this.finish();
        }
    }

    private apply(frameArr: any[]): void {
        var changed: Boolean = false;
        if (this.playground.numChildren != frameArr.length)
            changed = true;
        else
            for (var i: number = 0; i < this.playground.numChildren; i++) {
                var item: egret.Bitmap = <egret.Bitmap>this.playground.getChildAt(i);
                if (item.name != frameArr[i].texture) {
                    changed = true;
                    break;
                } else
                    this.setPos(item, frameArr[i].pos);
            }

        if (changed) {
            this.playground.removeChildren();
            for (var j: number = 0; j < frameArr.length; j++) {
                this.addItem(frameArr[j]);
            }
        }
    }

    private setPos(item: egret.Bitmap, obj: any): void {
        item.x = obj.x;
        item.y = obj.y;
        item.scaleX = obj.scaleX;
        item.scaleY = obj.scaleY;
        item.rotation = obj.rotation;
    }

    private addItem(obj: any): void {

        var item: egret.Bitmap = this.creatBM(obj.texture);
        item.name = obj.texture;

        this.setPos(item, obj.pos);
        this.playground.addChildAt(item, obj.index);
    }

    public initData(obj: PaintLoader): void {
        super.initData(obj);
        this.data = this.loader.animeData;

        this.totalTime = this.data.replayData.length / 24;
        this.timer = new egret.Timer(1000 / 24);

        var cover = this.creatBM(this.data.background);
        this.addChild(cover);

        this.playground = new egret.Sprite();
        this.addChild(this.playground);
    }

    private creatBM(key: string): egret.Bitmap {
        return new egret.Bitmap(this.getRes(key));
    }

    private data: any;
    private timer: egret.Timer;

    private playground: egret.Sprite;
    private totalTime: number;

    //private snd: egret.Sound;

    public dispose(): void {
        /*
        if (this.snd)
        {
            this.snd.stop();
            this.snd = null;
        }
        */
        SoundManager.stopSound();
        if (this.timer){
            this.timer.removeEventListener(egret.TimerEvent.TIMER, this.onTimer, this)
            this.timer.stop();
            this.timer = null;
        }
    }
}